import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AbstractDto } from './dto/create-post.dot';
import { Tags } from '../tags/entities/tags.entity';
import { Categories } from './../categories/entities/categories.entity';

@Injectable()
export class Abstract {
	// 使用InjectRepository装饰器并引入Repository这样就可以使用typeorm的操作了
  constructor(
    @InjectRepository(Abstract)
    private readonly abstractRepository: Repository<Abstract>,
  ) {}

	// 联查标签
  async tags(tagId: string): Promise<Tags | any> {
    let tags = await this.abstractRepository.query(
      `SELECT * FROM tags WHERE id in (${JSON.parse(tagId).join()})`,
    );
    return {
      tags: tags,
      num: tags.length,
    };
  }

	// 联查分类
  async categories(categoryId: string): Promise<Categories | any> {
    let categories = await this.abstractRepository.query(
      `SELECT * FROM categories WHERE id in (${ categoryId })`,
    );
    return {
      categories: categories,
      num: categories.length,
    };
  }
}
